package cn.edu.jxau.core.vo;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取List<Point>中的波峰波谷算法
 * @author 巫发萍
 * PretreatmentAlgorithm
 * 2017年8月9日
 */
public class Peaks {
	/**
	 * 获取最小峰谷
	 * @param lists
	 * @return
	 */
	public static Point getminPeaks(List<Point> lists){
		 List<List> listPeaks = getPeaks(lists);
		 List<Point> listminPeaks = listPeaks.get(0);
		 Point minPeaks = listminPeaks.get(0) ;
		 for(Point listminPeaks1 : listminPeaks){
			 if(minPeaks.getY() >= listminPeaks1.getY()){
				 minPeaks = listminPeaks1;
			 }
		 }
		 System.out.println("("+minPeaks.getX()+" , " +minPeaks.getY()+") 最小峰谷");
		return minPeaks;
	}
	
	/**
	 * 获取最大波峰
	 * @param lists
	 * @return
	 */
	public static Point getmaxPeaks(List<Point> lists){
		 List<List> listPeaks = getPeaks(lists);
		 List<Point> listmaxPeaks = listPeaks.get(1);
		 Point maxPeaks = listmaxPeaks.get(0) ;
		 for(Point listmaxPeaks1 : listmaxPeaks){
			 if(maxPeaks.getY() <= listmaxPeaks1.getY()){
				 maxPeaks = listmaxPeaks1;
			 }
		 }
		 System.out.println("("+maxPeaks.getX()+" , " +maxPeaks.getY()+") 最大波峰");
		return maxPeaks;
	}
	
	private static List<List> getPeaks(List<Point> lists){
		List<List> listPeaks = new ArrayList<List>();
		List<Point> listminPeaks = new ArrayList<Point>();
		List<Point> listmaxPeaks = new ArrayList<Point>();
		for(int i=1;i<lists.size()-1;i++) {
			if(lists.get(i-1).getY()<lists.get(i).getY()&&lists.get(i).getY()>lists.get(i+1).getY()){
				listmaxPeaks.add(lists.get(i));
			}else if(lists.get(i-1).getY()>lists.get(i).getY()&&lists.get(i).getY()<lists.get(i+1).getY()){
				listminPeaks.add(lists.get(i));
			}
		  }
		listPeaks.add(listminPeaks);
		listPeaks.add(listmaxPeaks);
		return listPeaks;
	}
}
